import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:name_card_android/main_screen_state.dart';
import 'package:name_card_android/card/contacts_screen.dart';
import 'package:name_card_android/home/home_screen.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:name_card_android/profile/profile_screen.dart';
import 'package:name_card_android/setting/setting_screen.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({super.key});

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> with AutomaticKeepAliveClientMixin {

  int _currentIndex = 0;
  late PageController _pageController;

  Map<int,Widget> pageList = { 0: HomeScreen(),1: ProfileScreen(), 2: ContactsScreen()};

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final localizations = AppLocalizations.of(context)!;
    return Scaffold(
      body: Consumer<MainScreenState>(
        builder: (context, state, child) {
          return PageView.builder(
            physics: const NeverScrollableScrollPhysics(),
            controller: _pageController,
            onPageChanged: (index){
              setState(() {
                _currentIndex = index;
              });
            },
            itemBuilder: (context,index){
              return pageList[index];
            },
          );
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: const Icon(Icons.home),
                label: localizations.homeScreenTitle,
              ),
              BottomNavigationBarItem(
                icon: const Icon(Icons.person),
                label: localizations.profileScreenTitle,
              ),
              BottomNavigationBarItem(
                icon: const Icon(Icons.contacts),
                label: localizations.contactsScreenTitle,
              ),
            ],
            currentIndex: _currentIndex,
            selectedItemColor: Theme.of(context).colorScheme.primary,
            onTap: (index) {
              _pageController.jumpToPage(index);
            },
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
